

/**
 * 保存执法人员
 * @param data
 * @param callback
 */
function saveStaffOffLine(data,callback){
	db.transaction(function (tx) {
    	tx.executeSql("INSERT INTO CASE_STAFF(" +
    			"STAFF_NAME,STAFF_CERTIFICATEID,ORGANID,ORGANNAME,CREATE_TIME) " +
    			"VALUES (?,?,?,?,?)",
    			[data.staffName==undefined ? '' : data.staffName  ,
    			 data.certificateId==undefined ? '' : data.certificateId  ,
				 data.organId==undefined ? '' : data.organId,
				 data.organName==undefined ? '' : data.organName,
				 data.createTime==undefined ? '' :data.createTime],
       function (tx, result) {
    	    if(typeof(callback) == 'function'){
    	    	callback(data);
    	    }
       },
	   function (tx, error) {
    	   message("执法人员保存失败");
	   });
    });
}
/**
 * 通过机构ID获取执法人员
 * @param organId 主键
 * @param callback 回调函数
 **/
function findStaffByOrganIdOffLine(organId,callback){
	db.transaction(function (tx) { 
		tx.executeSql("SELECT * FROM CASE_STAFF WHERE ORGANID = ? ORDER BY CREATE_TIME DESC", [organId], 
		function (tx, result){ 
/*		    var len = result.rows.length;  
		    if(len == 0){
		    	return;
		    }
		    
*/		    var data = setStaffDataOffLine(result);
    	    if(typeof(callback) == 'function'){
    	    	callback(data);
    	    }
	    }, 
	    function(tx, ex) {
        });  
	});
}

/**
 * 通过机构Id和执法人员证件号 获取执法人员
 * @param staffName
 * @param certificateId
 * @param organId
 * @param organName
 * @param callback
 */
function findStaffByCertificateIdAndOrganIdOffLine(staffName,certificateId,organId,organName,callback){
	db.transaction(function (tx) { 
		tx.executeSql("SELECT * FROM CASE_STAFF WHERE STAFF_CERTIFICATEID = ? AND ORGANID = ? ORDER BY CREATE_TIME DESC", [certificateId,organId], 
		function (tx, result){ 
		    var len = result.rows.length;  
		    if(len > 0){
		    	return;
		    }else{
		    	var time = currentTime();
		    	var staff={};
		    	staff.staffName=staffName;
		    	staff.certificateId=certificateId;
		    	staff.organId=organId;
		    	staff.organName=organName;
		    	staff.createTime=time;
		    	 if(typeof(callback) == 'function'){
		    	    	callback(staff);
		    	   }
		    }
	    }, 
	    function(tx, ex) {
        });  
	});
}

/**
 * 根据查询条件获取执法人员
 * @param condition 查询条件
 * @param page 分页
 * @param callback  回调函数
 **/
function findStaffTotalOffLine(condition,con,page){
	var sql = addCondition(condition);
	db.transaction(function (tx) { 
		tx.executeSql("SELECT COUNT(*) TOTAL FROM CASE_STAFF WHERE 1=1 " + sql, [], 
		function (tx, result){
		    var len = result.rows.length;
		    for (var i = 0; i < len; i++){
				page.data.total = result.rows.item(i).TOTAL;
		    }
		    /**
			 * 回调函数
			 */
			if(typeof(page.callback) == 'function'){
				page.callback();
			}
			$(con.datagrid).datagrid({
				border:false,
				fitColumns:true,
				checkbox:true,
				fit:true,
				remoteSort:false,
				rownumbers:false,
				singleSelect:con.singleSelect,
				toolbar: con.datagridtoolbar,
				pagination:con,
				loadFilter:pagerFilter}).datagrid('loadData', page.data);
	    }, 
	    function(tx, error) {
	    	message("获取执法人员列表失败:"+ error.message);
        });  
	});
}
/**
 * 根据查询条件 获取执法人员列表
 * @param condition 查询条件
 * @param page 分页
 **/
function findAllLStaffOffLine(condition,con,page){
	var sql = addCondition(condition);
	db.transaction(function (tx) { 
		
		
		/*ORDER BY CREATE_TIME DESC LIMIT ?,?*/ 
		tx.executeSql("SELECT * FROM CASE_STAFF WHERE 1=1" + sql +" ORDER BY STAFF_CERTIFICATEID ASC LIMIT ?,?", [(page.pageNumber)*page.pageSize,page.pageSize], 
		function (tx, result){
		    page.data.rows= setStaffDataOffLine(result);
		    findStaffTotalOffLine(condition,con,page);
	    }, 
	    function(tx, error) {
	    	message("获取执法人员列表失败:"+ error.message);
        });  
	});
}
/**
 * 初始化执法人员
 * @param db
 * @param callback
 **/
function initStaff(){
	staff(getStaffData);
}

/**
 * 清空数据
 */
function staff(callback,data){
	db.transaction(function (tx) { 
		tx.executeSql('DELETE FROM CASE_STAFF', [], 
		function (tx, results){
			if (typeof(callback) == 'function'){
				$.each(data,function(index,item){
					callback(item);
				});
			}
		}, 
		function (tx, ex){
			message("删除执法人员数据失败");
		}); 
	});
}


/**
 * 拼接sql语句
 * @param condition 查询条件
 **/
function addCondition(condition){
	
	var sqlWhere = "";
	
	/**
	 * 法规名称
	 **/
	if($.trim(condition.staffName).length != 0){
		sqlWhere += " AND STAFF_NAME LIKE '%" + condition.staffName + "%'";
	}
	
	/**
	 * 违章编码
	 **/
	if($.trim(condition.certificateId).length != 0){
		sqlWhere += " AND STAFF_CERTIFICATEID LIKE '%" + condition.certificateId + "%'";
	}
	
	/**
	 * 违法事实
	 **/
	if($.trim(condition.organName).length != 0){
		sqlWhere += " AND ORGANNAME LIKE '%" + condition.organName + "%'";
	}

	return sqlWhere;
}

/**
 * 组装数据list
 * @param result
 * @returns {Array}
 */
function setStaffDataOffLine(result){
	var len = result.rows.length;
    var data = new Array();
	for (var i = 0; i < len; i++){
		var staff = {};
		staff.id = result.rows.item(i).CASE_STAFF_ID;
		staff.staffName = result.rows.item(i).STAFF_NAME;
		staff.certificateId = result.rows.item(i).STAFF_CERTIFICATEID;
		staff.organId = result.rows.item(i).ORGANID;
		staff.organName = result.rows.item(i).ORGANNAME;
	    data.push(staff);
    }
	return data;
}

/**
 * 从服务器上获取
 **/
function getStaffData(staffData){
	if(staffData==null || staffData.length<=0){
		$.ajax({
			type : "post",
			url : getContextPath() + "/staff/getStaffAll",
			dataType : "json",
			data : {organId:$.cookie("organId")},
			error : function(request) {
				message("获取执法人员列表失败");
			},
			success : function(data) {
				$.each(data, function(i, item) {
					/**
					 * 添加到本地
					 **/
					saveStaffOffLine(item);
				});
			}
		});
	}
}